package org.deschutter;

/**
 * User: Berten De Schutter
 * Date: 30-Jun-2009
 * Time: 15:26:59
 */

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

public abstract class URLReader {

    public static String get(String urlString) throws MalformedURLException {
        String temp = "";
        URL url;
        url = new URL(urlString);

        try {
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                            url.openStream()));

            String inputLine;

            while ((inputLine = in.readLine()) != null)
                temp += inputLine;

            in.close();


        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return temp;
    }

    public static String copy(String urlString, String localfile) throws MalformedURLException {
        String temp = "";

        try {
            File newfile = new File(localfile);
            URL url = new URL(urlString);

            URLConnection urlC = url.openConnection();

            InputStream is = url.openStream();


            Date date = new Date(urlC.getLastModified());

            System.out.flush();
            FileOutputStream fos = null;

            fos = new FileOutputStream(localfile);
            int oneChar, count = 0;
            while ((oneChar = is.read()) != -1) {
                fos.write(oneChar);
                count++;
            }
            is.close();
            fos.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return temp;
    }
}
